﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CCS;
using CCSTest.Properties;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace CCSTest
{
    [TestClass]
    public class ServiceTests
    {
        #region Setup Code

        private readonly StorageAccountInfo accountInfo = new StorageAccountInfo(Settings.Default.TableStorageEndpoint,
                                                                                 true, Settings.Default.AccountName,
                                                                                 Settings.Default.AccountSharedKey);

        private readonly Guid devToken = Guid.NewGuid();
        private readonly TableStorage tableStorage;

        public ServiceTests()
        {
            tableStorage = TableStorage.Create(accountInfo);
            tableStorage.RetryPolicy = RetryPolicies.RetryN(10, TimeSpan.FromSeconds(1));
        }

        private CCSProduct _GetProduct()
        {
            CCSProduct retval = Procedures.InsertNewProduct(tableStorage,
                                                            string.Format("Test {0}", DateTime.UtcNow.Ticks),
                                                            DateTime.Now.AddDays(3), Guid.NewGuid().ToString(), 3, 0, 0,
                                                            0, 0, 0, devToken);
            Assert.IsNotNull(retval);
            return retval;
        }


        private string _GetInvoice(CCSProduct product)
        {
            CCSLicenseLevelPrice licenseLevelPrice = Procedures.GetProductLicenseLevels(accountInfo, product.ProductName,
                                                                                        product.DevToken);
            Assert.IsNotNull(licenseLevelPrice);

            string invoice = Guid.NewGuid().ToString();

            CCSLicenseTransaction licenseTransaction = Procedures.StoreLicenseTransaction(tableStorage, invoice,
                                                                                          string.Empty, product.ID,
                                                                                          licenseLevelPrice.LicenseID,
                                                                                          product.DevToken, string.Empty);
            Assert.IsNotNull(licenseTransaction);

            PaymentTransaction paymentTransaction = Procedures.Payment_Insert_Transaction(tableStorage, DateTime.Now,
                                                                                          string.Empty,
                                                                                          Guid.NewGuid().ToString(),
                                                                                          licenseTransaction.Invoice,
                                                                                          string.Empty, 0, 0,
                                                                                          string.Empty, DateTime.Now,
                                                                                          "completed", string.Empty,
                                                                                          string.Empty, string.Empty,
                                                                                          string.Empty, 0, 0,
                                                                                          string.Empty, 0, string.Empty,
                                                                                          string.Empty);
            Assert.IsNotNull(paymentTransaction);

            PaymentShoppingItems paymentShoppingItems = Procedures.Payment_Insert_Shopping_Items(tableStorage,
                                                                                                 paymentTransaction.ID,
                                                                                                 product.ProductName,
                                                                                                 product.ID.ToString(),
                                                                                                 string.Empty,
                                                                                                 string.Empty,
                                                                                                 string.Empty,
                                                                                                 string.Empty);
            Assert.IsNotNull(paymentShoppingItems);

            PaymentCustInfo paymentCustInfo = Procedures.Payment_Insert_CustInfo(tableStorage, paymentTransaction.ID,
                                                                                 "114 Alaskan Way South #104",
                                                                                 string.Empty, "Office", string.Empty,
                                                                                 string.Empty, "Seattle", "US",
                                                                                 "206-409-9940", string.Empty,
                                                                                 string.Empty, "tyler@iaspace.com",
                                                                                 "Tyler", "Schlegel", string.Empty,
                                                                                 string.Empty, string.Empty,
                                                                                 string.Empty, string.Empty, "WA",
                                                                                 "98104");
            Assert.IsNotNull(paymentCustInfo);

            return paymentTransaction.Invoice;
        }

        #endregion
        [TestMethod]
        public void Test_RegisterUser()
        {
            var devToken = Guid.NewGuid();
            string productName = string.Format("Test {0}", DateTime.UtcNow.Ticks);
            CCSProduct product = Procedures.InsertNewProduct(tableStorage,
                                                 string.Format(productName, DateTime.UtcNow.Ticks),
                                                 DateTime.Now.AddDays(3), Guid.NewGuid().ToString(), 1, 0, 0,
                                                 0, 0, 0, devToken);

            CCS.CCS ccs = new CCSTest.CCS.CCS();
            bool registered = ccs.RegisterUser("Schlegel", "Tyler", "114 Alaskan Way #104", "Seattle", "WA", "98201", "425-231-6082", "schleg@gmail.com", devToken.ToString(), productName, Guid.NewGuid().ToString(), "Shareware");
            Assert.IsTrue(registered);
        }
    }
}